package com.zhu.index.controller;

import com.zhu.mapper.UserNameDAO;
import com.zhu.mapper.annotation.UserIDDAO;
import com.zhu.mapper.annotation.autoMapping.UserDAO;
import com.zhu.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DELL on 2015/12/22.
 */
@Controller
@RequestMapping(value={"/", "/index"})
public class IndexController {

    private Map<String, byte[]> fileMap = new HashMap<String, byte[]>();

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserIDDAO userIdDAO;

    @Autowired
    private UserNameDAO userNameDAO;

    @Autowired
    private UserNameDAO userNameDAOWithSupport;

    @PostConstruct
    public void init(){
        System.out.println("IndexController init:" + this);
    }

    @RequestMapping
    public String index(HttpServletRequest request, HttpServletResponse response, Model model){
        model.addAttribute("name", "zhu");
        System.out.println("IndexController init1:" + this);

        UserNameDAO dao = ContextUtil.getBean("userNameDAO", UserNameDAO.class);
        System.out.println("----------------------------");
        if(userDAO != null){
            List<String> nameList = userDAO.getUserNameList();
            if(nameList != null){
                for(String name : nameList){
                    System.out.println(name);
                }
            }else{
                System.out.println("nameList is empty");
            }
        }else {
            System.out.println("userDao is null");
        }

        System.out.println("----------------------------");
        if(userIdDAO != null){
            List<Long> idList = userIdDAO.getUserIds();
            if(idList != null){
                for(long id : idList) {
                    System.out.println(id);
                }
            }else{
                System.out.println("idList is null");
            }
        }

        System.out.println("----------------------------");
        if(userNameDAO != null){
            String name = userNameDAO.getUserName(10081);
            System.out.println("nams:" + name);
        }

        System.out.println("----------------------------");
        if(userNameDAOWithSupport != null){
            String name = userNameDAOWithSupport.getUserName(1);
            System.out.println("nams:" + name);
        }

        return "css/3d";
    }

    @RequestMapping("sys")
    public String uploadFileTest(HttpServletRequest request, HttpServletResponse response){
        return "wysiwyg/test";
    }

    @RequestMapping("web/ajaxUploadFile")
    @ResponseBody
    public String upload(HttpServletRequest request, HttpServletResponse response){
        String fileKey = String.valueOf(System.currentTimeMillis());

        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        MultipartFile file =  multiRequest.getFile("editorImage");
        if(file != null){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedOutputStream bufout = new BufferedOutputStream(out);
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(file.getInputStream());
                int len = -1;
                byte[] buf = new byte[1024];
                while((len = in.read(buf)) != -1) {
                    bufout.write(buf, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    bufout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            fileMap.put(fileKey, out.toByteArray());
        }

        return request.getContextPath() + "/web/requestFile?fileKey=" + fileKey;
    }

    @RequestMapping("web/requestFile")
    public void requestFile(@RequestParam("fileKey") String fileKey, HttpServletRequest request, HttpServletResponse response){
        if(fileKey != null && fileMap.containsKey(fileKey)) {
            byte[] fileBuf = fileMap.get(fileKey);
            response.setHeader("Content-Type", "image/jped");

            byte[] buf = new byte[1024];
            BufferedInputStream in = null;
            try {
                in = new BufferedInputStream(new ByteArrayInputStream(fileBuf));
                int len = -1;
                while((len = in.read(buf)) != -1) {
                    response.getOutputStream().write(buf, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
